using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class HomeManager : MonoBehaviour
{
	[Serializable]
	public class EditorReferences
	{
		public HomeLevel level;

		public HomePseudo pseudo;

		public HomeSettings settings;

		public HomeSkins skins;

		public HomeModes modes;

		public HomePlay play;

		[Space]
		public HomeHole hole;
	}

	public static HomeManager Instance;

	[SerializeField]
	public EditorReferences references = new EditorReferences();

	private float _startHomeTime;

	private void Awake()
	{
		Instance = this;
	}

	private void Start()
	{
		InitializeLevel();
		//InitializePseudo();
		InitializePlay();
		InitializeHole();
	//	VoodooServiceLocator.Ads.ShowGDPRBanner();
	//	VoodooServiceLocator.Ads.ShowBanner(delegate
		//{
		//});
		//VoodooServiceLocator.Ads.ShowCrossPromo();
		_startHomeTime = Time.realtimeSinceStartup;
	}

	private void InitializeLevel()
	{
		int cachedRank = RankManager.Instance.CachedRank;
		references.level.SetRank(cachedRank, RankManager.Instance.CachedXp, RankManager.Instance.GetRankParameters(cachedRank - 1), RankManager.Instance.GetRankParameters(cachedRank));
		if (RankManager.Instance.XpReward > 0)
		{
			references.level.SetReward(RankManager.Instance.XpReward);
			RankManager.Instance.CashInXpReward();
			if (cachedRank != RankManager.Instance.CachedRank)
			{
				cachedRank = RankManager.Instance.CachedRank;
				references.level.LevelUpRank(cachedRank, RankManager.Instance.CachedXp, RankManager.Instance.GetRankParameters(cachedRank - 1), RankManager.Instance.GetRankParameters(cachedRank));
			}
			else
			{
				references.level.UpdateRank(RankManager.Instance.CachedXp, RankManager.Instance.GetRankParameters(cachedRank));
			}
		//	VoodooServiceLocator.Ads.ShowRater();
		}
	}

	//private void InitializePseudo()
	//{
	//	if (LocalizationManager.GetLanguage() == "Chinese")
	//	{
	//		if (PlayerData.Instance.Pseudo == "Player")
	//		{
	//			PlayerData.Instance.Pseudo = "高手留个名吧";
	//			PlayerData.Instance.SaveData();
	//		}
	//	}
	//	else if (PlayerData.Instance.Pseudo == "高手留个名吧")
	//	{
	//		PlayerData.Instance.Pseudo = "Player";
	//		PlayerData.Instance.SaveData();
	//	}
	//	references.pseudo.SetPseudo(PlayerData.Instance.Pseudo);
	//	references.pseudo.SetColor(SkinManager.Instance.GetSkinWithIndex(App.Instance.currentSkinIndex).mainColor);
	//}

	private void InitializePlay()
	{
		references.play.SetGameMode(GameModeManager.Instance.CurrentMode);
		references.play.UpdateBestScore();
		//if (VoodooServiceLocator.Ads.IsPremium())
		//{
			references.play.CenterPlayButton();
		//}
	}

	private void InitializeHole()
	{
		references.hole.SetSkin(SkinManager.Instance.GetSkinWithIndex(App.Instance.currentSkinIndex));
		references.hole.PlaySkinFX();
	}

	public void OnChangeGameMode()
	{
		references.play.SetGameMode(GameModeManager.Instance.CurrentMode);
		references.play.UpdateBestScore();
		PlayerData.Instance.LastMode = GameModeManager.Instance.CurrentMode.id;
		PlayerData.Instance.SaveData();
	}

	public void OnChangeSkin()
	{
		references.hole.SetSkin(SkinManager.Instance.GetSkinWithIndex(App.Instance.currentSkinIndex));
		references.pseudo.SetColor(SkinManager.Instance.GetSkinWithIndex(App.Instance.currentSkinIndex).mainColor);
		PlayerData.Instance.Skin = App.Instance.currentSkinIndex;
		PlayerData.Instance.SaveData();
	}

	public void Play()
	{
		if (!(Time.realtimeSinceStartup - _startHomeTime < 2f))
		{
			App.Instance.enableBigStart = false;
			LaunchGame();
		}
	}

	public void PlayBigger()
	{
		if (!(Time.realtimeSinceStartup - _startHomeTime < 2f))
		{
			App.Instance.enableBigStart = true;
			LaunchGame();
		}
	}

	private void LaunchGame()
	{
		if (PlayerData.Instance.Xp < 5)
		{
			MapManager.Instance.CurrentMap = MapManager.Instance.references.defaultMap;
		}
		else
		{
			MapManager.Instance.CurrentMap = MapManager.Instance.GetRandomMap();
		}
		AchievementManager.Instance.ResetTemporaryData();
		PlayerData.Instance.LastMap = MapManager.Instance.CurrentMap.id;
		PlayerData.Instance.SaveData();
		//VoodooServiceLocator.Ads.HideCrossPromo();
		string gameScene = GameModeManager.Instance.CurrentMode.gameScene;
		SceneManager.LoadScene(gameScene);
	}

	public void DisplayHomeUI(bool state)
	{
		references.hole.DisplayWithAnimation(state);
		//references.settings.DisplayWithAnimation(state);
		references.level.DisplayWithAnimation(state);
		references.skins.DisplayWithAnimation(state);
		references.modes.DisplayWithAnimation(state);
		references.pseudo.gameObject.SetActive(state);
		references.play.gameObject.SetActive(state);
		if (state)
		{
			//VoodooServiceLocator.Ads.ShowCrossPromo();
			references.hole.PlaySkinFX();
		}
		else
		{
			//VoodooServiceLocator.Ads.HideCrossPromo();
			references.hole.StopSkinFX();
		}
	}

	public void TEST_AddXp(int xp = 50)
	{
		int cachedRank = RankManager.Instance.CachedRank;
		RankManager.Instance.AddXpReward(xp);
		RankManager.Instance.CashInXpReward();
		if (cachedRank != RankManager.Instance.CachedRank)
		{
			cachedRank = RankManager.Instance.CachedRank;
			references.level.LevelUpRank(cachedRank, RankManager.Instance.CachedXp, RankManager.Instance.GetRankParameters(cachedRank - 1), RankManager.Instance.GetRankParameters(cachedRank));
		}
		else
		{
			references.level.UpdateRank(RankManager.Instance.CachedXp, RankManager.Instance.GetRankParameters(cachedRank));
		}
	}
}
